/**
 * User: alesj
 * Date: 2005.3.18
 * Time: 13:03:16
 * 
 * (C) Genera Lynx d.o.o.
 */

package com.generalynx.common.data;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;

import java.lang.reflect.Method;

public class JdbcFactoryAdviceCreator  {

    private IJdbcFactory jdbcFactory;

    public JdbcFactoryAdviceCreator(IJdbcFactory jdbcFactory) {
        this.jdbcFactory = jdbcFactory;
    }

    public MethodBeforeAdvice createBeforeAdvice() {
        return new MethodBeforeAdvice() {
            public void before(Method method, Object[] args, Object target) throws Throwable {
                ThreadJdbcFactoryHolder.setJdbcFactory(jdbcFactory);
            }
        };
    }

    public AfterReturningAdvice createAfterAdvice() {
        return new AfterReturningAdvice() {
            public void afterReturning(Object returnValue, Method method, Object[] args, Object target)
                    throws Throwable {
                ThreadJdbcFactoryHolder.clear();
            }
        };
    }

    public ThrowsAdvice createThrowsAdvice() {
        return new JdbcThrowsAdvice();
    }

}
